<template>
  <div class="center-wrapper">
    <!-- 查询模式选择 -->
    <div id="query-mode">
      <span>查询模式选择</span>
      <ul class="mode">
        <li
          class="pointerEvents"
          @click.stop="renderClowed()"
          :style="{
            color: isShowHeat ? 'aqua' : '',
          }"
        >
          <i class="iconfont metro-lineRoute"></i> <span>站点拥挤度</span>
        </li>
        <li @click.stop="isShowRoute()"  :style="{ color: isShowRouteDesign ? 'aqua' : '',}"><i class="iconfont metro-lujingguihua"></i><span>路径规划</span></li>
        <li
          class="pointerEvents"
          @click.stop="renderStationMeasure()"
          :style="{
            color: isShowMeasure ? 'aqua' : '',
          }"
        >
          <i class="iconfont metro-ditie"></i> <span>站控措施</span>
        </li>
      </ul>
    </div>
    <!-- 路径规划 -->
    <RouteDesign v-if="isShowRouteDesign" />
    <!-- 站控措施 -->
    <div id="station-measure">
      <span>图标 站控措施</span>
      <ul class="mode">
        <li v-for="(item, index) in station_solutions" :key="index">
          <i :class="['iconfont',item.iconName]" ></i> <span>{{ item.title }}</span>
        </li>
      </ul>
    </div>
    <!-- 地铁线路 -->
    <div id="subLine-controller">
      <div class="title-bg">
        <i class="iconfont metro-lineRoute"></i> 地铁线路
      </div>
      <div class="item-wrapper">
        <div
          class="item"
          v-for="(item, index) in subLineData"
          :key="index"
          @click.stop="handleItemClick(item, index)"
        >
          <div
            class="box"
            :style="{
              borderColor: item.color,
              backgroundColor:
                index === itemIndex ? item.color : 'rgba(0,0,0,0)',
            }"
          ></div>
          <span :style="{ color: '#34c5cf' }">{{ item.name.slice(-3) }}</span>
        </div>
      </div>
    </div>
    <!-- 站点列表 -->
    <div id="station-controller">
      <div class="title-bg"><i class="iconfont metro-ditie"></i> 地铁站点</div>
      <div class="item-wrapper">
        <div
          class="item"
          :key="index"
          v-for="(item, index) in stationData"
          @click.stop="handleActiveItem(item, index)"
        >
          <div
            class="box"
            :style="{
              backgroundColor: index === metroIndex ? '#fff' : 'rgba(0,0,0,0)',
            }"
          ></div>
          <span :style="{ color: '#34c5cf' }">{{ item.name }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { onMounted, ref, watch } from 'vue';
import RouteDesign from './RouteDesgin.vue'
import {
  flyToLine,
  binkLineByName,
  flytoStationByName,
  renderHeat,
} from '@/cesiumTools/effectController';
import { useLineData, useMeasureData } from '@/store';

const linedata = useLineData().lineData;
const stationData = ref([]);
const subLineData = ref(linedata);
const metroIndex = ref();
const itemIndex = ref();
const isShowHeat = ref(false);
const isShowMeasure = ref(false);
const isShowRouteDesign = ref(false);
const station_solutions = ref([
       {iconName: 'metro-yibao',title:'重保'},
       {iconName:'metro-yonghu',title:'分批放行乘客'},
       {iconName:'metro--control',title:'安检'},
       {iconName:'metro-jinzhi',title:'封站'},
        {iconName:'metro-code-branch',title:'引导乘客分流'},
        {iconName:'metro-fence-full',title:'设立导流围栏'},
        {iconName:'metro-shoupiao',title:'减缓售票速度'},
        {iconName:'metro-dianti',title:'改变电梯运行方向'},
    ])
const viewer = useLineData().viewer;
const measureDataStore = useMeasureData();
const handleItemClick = (item, index) => {
  itemIndex.value = index;
  console.log(item);
  const { name, stationsList, color, id } = item;
  flyToLine(viewer, name);
  binkLineByName(name);
  stationData.value = stationsList;
};
const handleActiveItem = (item, index) => {
  metroIndex.value = index;
  flytoStationByName(viewer, item.name);
};
let removeHeat;
const renderClowed = () => {
  isShowHeat.value = !isShowHeat.value;
  console.log(linedata);
  if (removeHeat) {
    removeHeat();
    removeHeat = null;
  } else {
    // 解析线路拥挤度数据
    const dataSource = [];
    // 清除当前底图，然后添加arcgis底图
    subLineData.value.forEach((item) => {
      const { stationsList } = item;
      stationsList.forEach((s) => {
        const { position } = s;
        dataSource.push({
          x: position.lng,
          y: position.lat,
          value: Math.ceil(Math.random() * 1000),
        });
      });
    });
    removeHeat = renderHeat(viewer, dataSource);
  }
};

//站控措施
const renderStationMeasure = () => {
  isShowMeasure.value = !isShowMeasure.value;
  function getRandomIntInclusive(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值，含最小值
  }

  // 从数组中随机取几个数
  const getRandomArrayValue = (arr, num) => {
    var sData = arr.slice(0),
      i = arr.length,
      min = i - num,
      item,
      index;
    while (i-- > min) {
      index = Math.floor((i + 1) * Math.random());
      item = sData[index];
      sData[index] = sData[i];
      sData[i] = item;
    }
    return sData.slice(min);
  };

  const resultData = [];
  linedata.forEach((item) => {
    const { stationsList, color } = item;
    const result = stationsList.map((s) => {
      // 0-5个站控措施
      const measureNum = getRandomIntInclusive(0, 5);
      let measures = getRandomArrayValue(station_solutions.value, measureNum);
      s.measures = measures;
      s.color = color;
      return {
        name: s.name,
        measures,
      };
    });
    resultData.push(result);
  });
  // 全局共享站控措施数据
  if (isShowMeasure.value) {
    measureDataStore.setData(resultData);
  } else {
    measureDataStore.clearData();
  }
};
const isShowRoute = ()=>{
  isShowRouteDesign.value = !isShowRouteDesign.value
  if(isShowMeasure.value){
    isShowMeasure.value = false;
    measureDataStore.clearData();
  }
  if (removeHeat) {
    removeHeat();
    removeHeat = null;
    isShowHeat.value = false;
  }
  const pointerEvents = document.querySelectorAll('.pointerEvents');
  if(isShowRouteDesign.value){
    pointerEvents.forEach((item) => {
      item.style.pointerEvents = 'none';
    })
  }else{
    pointerEvents.forEach((item) => {
      item.style.pointerEvents = 'all';
    })
  }
}
</script>
<style scoped>
.wrapper {
  width: 100%;
  height: 100%;
}
/* 查询选择 */
#query-mode {
  position: absolute;
  top: 5%;
  left: 50px;
  width: 180px;
  height: 200px;
  font-size: 16px;
  color: #fff;
  cursor: pointer;
  z-index: 1000;
}
#station-measure{
  position: absolute;
  top: 5%;
  right: 50px;
  width: 180px;
  height: 200px;
  font-size: 16px;
  color: #fff;
  cursor: pointer;
  z-index: 999;
}
.mode {
  margin-top: 10px;
}
.mode li {
  display: flex;
  gap: 10px;
  list-style: none;
  margin-top: 10px;
}

.mode li i {
  background: url('/src/assets/uiResources/icon-wrapper.png');
  background-size: contain;
  width: 30px;
  height: 30px;
  display: inline-block;
  font-size: 14px;
  text-align: center;
  line-height: 30px;
}
.mode li span {
  background: url('/src/assets/uiResources/button.png');
  background-repeat: no-repeat;
  padding: 0 10px;
  height: 30px;
  display: inline-block;
  font-size: 14px;
  text-align: center;
  line-height: 30px;
  color: #fff;
  border-radius: 5px;
  cursor: pointer;
}
#station-measure .mode li span{
  font-size: 11px;
}
.mode li span:hover {
  color: rgb(255, 221, 0);
}
#station-measure .mode li span:hover{
  color: #fff;
}
/* 地铁线路列表 */
#subLine-controller {
  position: absolute;
  bottom: 100px;
  left: 32.5%;
  transform: translateX(-50%);
  width: 180px;
  height: 200px;
  background-color: rgba(0, 0, 0, 0.3);
  border: 1px solid #ab7818;
  display: flex;
  flex-direction: column;
}

.box {
  width: 10px;
  height: 10px;
  border-width: 1px;
  border-style: solid;
  background: transparent;
  user-select: all;
  cursor: pointer;
  transition: all 0.3s linear;
}

.item {
  width: 64.992px;
  height: 20.006px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0.156vw;
  background-color: rgba(255, 255, 255, 0.2);
  border: 1px solid #885f12;
  color: #fff;
  font-size: 0.521vw;
  cursor: pointer;
  pointer-events: all;
}

.item:hover {
  border: 1px solid #d8961a;
}

.item > span {
  line-height: 0.469vw;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 2.083vw;
}

.item > input {
  outline: none;
  border: none;
  transition: all 0.3s ease;
}

.item-wrapper {
  display: flex;
  justify-content: space-around;
  align-content: space-around;
  flex-wrap: wrap;
  flex: 1;
  padding: 4px;
  overflow: hidden;
}

/* 站点列表 */
#station-controller {
  width: 600px;
  height: 200px;
  border: 1px solid #ab7818;
  background-color: rgba(0, 0, 0, 0.3);
  position: absolute;
  left: 38%;
  bottom: 100px;
  color: #fff;
  display: flex;
  flex-direction: column;
}

#station-controller .item-wrapper {
  justify-content: flex-start;
}

#station-controller .item {
  margin: 0 7px;
}

.title-bg {
  width: 100%;
  height: 30px;
  background: url('/src/assets/uiResources/title-bg.png');
  background-size: contain;
  color: #fff;
  background-repeat: no-repeat;
  line-height: 30px;
  margin-left: 10px;
  background: rgb(255, 255, 255);
  background-image: linear-gradient(
    180deg,
    rgba(255, 255, 255, 1) 9%,
    rgba(211, 156, 50, 1) 57%
  );
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* 站控措施图例 */
.right-wrapper {
  width: 11.417vw;
  height: 100%;
  position: absolute;
  right: 0px;
  top: 2.083vw;
}

.right-wrapper .query-item-title {
  cursor: default;
  pointer-events: none;
}

.right-wrapper .query-item-title::after {
  width: 20px;
  left: -20px;
}

.legend .query-item i {
  pointer-events: none;
  cursor: default;
}

.title {
  color: #fff;
}
</style>
